package api

import (
	"fmt"
	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
	"net/http"
)

type fileApi struct {
}

func (a fileApi) FileUpload(c *gin.Context) {
	f, err := c.FormFile("fileKey")
	if err != nil {
		log.Error(err)
	}
	log.Debug(f.Filename)
	dst := "./" + f.Filename
	err = c.SaveUploadedFile(f, dst)
	if err != nil {
		log.Error(err)
	}

	c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", f.Filename))
}

func (a fileApi) MultipartFileUpload(c *gin.Context) {
	form, err := c.MultipartForm()
	if err != nil {
		log.Error(err)
	}
	files := form.File["upload[]"]
	for _, file := range files {
		log.Println(file.Filename)
		dst := "./" + file.Filename
		// 上传文件至指定目录
		c.SaveUploadedFile(file, dst)
	}
	c.String(http.StatusOK, fmt.Sprintf("%d files uploaded!", len(files)))
}
